<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends User_Controller {
    
    function Upload(){
        parent::User_Controller( );
        
        $this->load->helper( array( 'form', 'url' ) );
        
        // Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'Upload' ), 'article/user/upload' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Media' );

		// Load the article model
		$this->load->model( 'upload_model' );
		$this->load->model( 'website_model' );
		
		// Load the upload language module
		// @todo: doesn't work ???
		$this->lang->module_load( 'article', 'upload' );
		
		log_message( 'debug', 'conlabz CMS : Upload class loaded' );
    }
    
	function index( ){
		$folder_id = $this->uri->segment( 6 );
		if( empty( $folder_id ) ) {
			$folder_id = 0;
		}
		$data['folder_id'] = $folder_id;
		$data['folder_name'] = $this->upload_model->getFolderTitleById( $folder_id );
		$data['folder'] = $this->upload_model->getFolder( $data['folder_id'] );
		$data['files'] = $this->upload_model->getFiles( $data['folder_id'] );
		$website = $this->session->userdata( 'template' ); 
    	$parent = '';
    	$folder_name = '';
    	if( $folder_id != 0 ){
    		$folder_name = $this->upload_model->getFolderById( $folder_id ) . '/';
    	}
        $data['path'] = DEVPATH.'uploads/' . $website . $folder_name; //server directory
        // Display Page
		$data['header'] = $this->lang->line( 'upload_overview' );
		$data['page'] = $this->config->item('backendpro_template_user') . "/view_upload";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
    }
    
    function form( ){
    	$data['folder_id'] = $this->uri->segment( 6 );
    	// Include uplaod Javascript code
		$this->bep_assets->load_asset('jquery.MultiFile.min');
        // Display Page
		$data['header'] = $this->lang->line( 'upload_overview' );
		$data['page'] = $this->config->item('backendpro_template_user') . "/form_upload";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
    }

    function do_upload( ){
    	$website = $this->session->userdata( 'template' );
    	$parent = '';
    	$folder_name = '';  
    	$folder_id = (int) $this->input->post( 'folder_id' );
    	
    	if( $folder_id != 0 ){
    		$folder_name = $this->upload_model->getFolderById( $folder_id ) . '/';
    	}
    	
        $config['upload_path'] = './uploads/' . $website . $folder_name; //server directory
        $config['allowed_types'] = 'swf|pdf|gif|jpg|png'; // by extension, will check for whether it is an image, image at least
        $config['max_size']    = '1000'; // in kb
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library( 'upload', $config );
        $this->load->library( 'Multi_upload' );
    
        $files = $this->multi_upload->go_upload( );
        
        if ( !$files ){
	    	// Display Page
			$data['header'] = $this->lang->line( 'upload_overview' );
			$data['page'] = $this->config->item('backendpro_template_user') . "/form_upload";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$data['error'] = array('error' => "Needs to be done: ".$this->upload->display_errors() );
			$data['folder_id'] = $folder_id;
			$this->load->view( $this->_container, $data );
        } else {
        	//success 
        	//write into database
        	foreach( $files as $key => $data ){ 
        		$data['folder_id'] = $folder_id;
        		$data['title'] = substr( $data['name'], 0, strpos( $data['name'], "." ) );
        		$this->upload_model->insertFiles( $data );
        	} //foreach
        	
            $data = array( 'upload_data' => $files );
            // Display Page
			$data['header'] = $this->lang->line( 'upload_overview' );
			$data['page'] = $this->config->item('backendpro_template_user') . "/success_upload";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
        }
    } 

	/**
	 * Get Folder Details
	 *
	 * Load folder detail values from the submitted form and calcute folder name from title
	 *
	 * @access private
	 * @return array
	 */
	function _get_folder_details(){
		$data['title'] = $this->input->post( 'title' );
		$data['folder'] = strtolower( str_replace( array( " " ), array( '' ), $data['title'] ) );
		$data['parent'] = $this->input->post( 'folder_id' );
		$data['folder_name'] = $this->upload_model->getFolderById( $this->input->post( 'folder_id' ) );
		
		return $data;
	}
    
    function create_folder( ){
    	$website = $this->session->userdata( 'template' ).'/';
    	$parent = '';    	
        $path = './uploads/' . $website; // server directory

        $data = array( );
        $data = $this->_get_folder_details( );
        $data['website_id'] = $this->session->userdata( 'website_id' );
        
        //insert into database
        if( !empty( $data['folder_name'] ) ){
        	$parent = $data['folder_name'] . "/";
        }
        unset( $data['folder_name'] );
        $this->upload_model->insertFolder( $data );
        
        //mkdir on file system
        mkdir( $path . $parent . $data['folder'], 0777 );
        
        redirect( 'article/user/upload/' );
    } 
    
	/**
	 * Delete folder
	 *
	 * Deletes a folder with all files in it, can't be undo, so be careful
	 *
	 * @access public
	 */
	function delete( $id ){
		
		$this->upload_model->delete( $id );
		
		flashMsg( 'success', $this->lang->line( 'folder_deleted' ) );
		redirect( 'article/user/upload', 'location' );
	}
    
	/**
	 * Delete files
	 *
	 * Delete a single from the system and database
	 *
	 * @access public
	 */
	function delete_file( $id ){
		
		$this->upload_model->delete_file( $id );
		
		flashMsg( 'success', $this->lang->line( 'image_deleted' ) );
		redirect( 'article/user/upload', 'location' );
	}
}
?>
